<?php
namespace app\mobile\controller;

use app\model\Record; 
use app\model\Theme; 
use app\model\Worker; 
use app\model\Relation; 

class Formsaveinfo extends Base
{
    public function Formsaveinfo()
    {
global $_G, $_P;
$fans = $this->checkuser();
$themeid = intval($_P['themeid']);
if($themeid < 0){
	return_msg('管理后台地址不正确');
}
$system=$_G['system'];
$theme = Theme::getThemeById($themeid);
$config = 	Theme::getConfig($themeid);
if(empty($theme['id'])){
	return_msg('管理后台地址不正确');
}
if ($_P['op'] == 'save') {

    $isupdaterelation=false;
    $relation = Relation::getByThemeId($themeid,$_G['openid']);
    if(!empty($relation)){
        $fromuid = $relation['parentid'];
        $fopenid = $relation['popenid'];
        $workerid = $relation['workerid'];
    }

    $myworker = Worker::getThemeWorkerByOpenid($_G['openid'],$themeid,1);
    if (empty($myworker)){
        //如果来源为员工,则业绩给员工
        if (!empty($fromuid)){
            $theyworker = Worker::getThemeWorkerByUid($fromuid,$themeid,1);
            if (!empty($theyworker)){
                $fromuid = $theyworker['uid'];
                $fopenid = $theyworker['openid'];
                $workerid = $theyworker['uid'];
                $isupdaterelation=true;
            }
        }
        //如果来源为空,则给员工
        if (empty($fromuid) && !empty($workerid)){
            $theyworker = Worker::getThemeWorkerByUid($workerid,$themeid,1);
            $fromuid = $theyworker['uid'];
            $fopenid = $theyworker['openid'];
            $isupdaterelation=true;
        }
        //如果员工为空,则给来源所属员工
        if (!empty($fopenid) && empty($workerid)){
            $theyfrom = Relation::getByThemeId($themeid,$fopenid);
            $workerid = $theyfrom['workerid'];
            $isupdaterelation=true;
        }

        //在没有开启附近团的情况,当团员报名时,员工业绩归属与团长一致
        if ($config['isrecent']==0 && $recordid > 0 && $joinid == 1){
            $record = Record::getAttendGroupById($recordid);
            if (!empty($record['workerid'])){
                $workerid = $record['workerid'];
                $isupdaterelation=true;
            }
        }
        if ($isupdaterelation==true){
                Relation::update($relation['id'],array('parentid' => $fromuid,'popenid' => $fopenid,'workerid' => $workerid));
        }
    }else{
        $fromuid = $_G['member']['uid'];
        $fopenid = $_G['openid'];
        $workerid = $_G['member']['uid'];
    }

	$username = $_P['username'];
	$mobile = $_P['mobile'];
	$option = $_P['shoptitle'];
	if (is_array($option)) {
		$option = implode('|', $option);
	}
	$address = $_P['address'];
	$datetime = $_P['datetime'];

	$username=preg_replace('#[^\x{4e00}-\x{9fa5}A-Za-z0-9_,，\-]#u','',$username);
    $mobile=preg_replace('#[^0-9]#u','',$mobile);
    $option=preg_replace('#[^\x{4e00}-\x{9fa5}A-Za-z0-9_,，\-]#u','',$option);
    $address=preg_replace('#[^\x{4e00}-\x{9fa5}A-Za-z0-9_,，\-]#u','',$address);
    $datetime=preg_replace('#[^\x{4e00}-\x{9fa5}A-Za-z0-9_,，\-]#u','',$datetime);
	if(empty($username)){
	    return_msg('姓名为空');
	}elseif(empty($mobile)){
	    return_msg('电话为空');
	}elseif(mb_strlen($mobile,"utf-8")!=11){
	    return_msg('请输入11位手机号');
    }

	$uncode = $themeid.'-'.TIMESTAMP.rand(1000,1000000);

    $data = array(
        'uniacid'  => $_G['mpid'],
        'themeid'  => $themeid,
        'openid'   => $_G['openid'],
        'uid'	   => $_G['member']['uid'],
        'nickname' => $fans['nickname'],
        'avatar'   => empty($fans['avatar']) ? $fans['headimgurl']:$fans['avatar'],
        'username' => $username,
        'mobile'   => $mobile,
        'addtime'  => TIMESTAMP,
        'fee'      => 0,
        'option' => $option,
        'address' => $address,
        'datetime' => $datetime,
        'code'     => '',
        'uncode'   => $uncode,
        'status'   => 0,
        'fromuid'  => $fromuid,
        'fopenid'  => $fopenid,
        'workerid'  => $workerid,
        'pid'      => 0,
        'type'     => 0,
        'renshu'     => 0,
    );

    $addtime = TIMESTAMP-1200;
    $sql = "SELECT * FROM " . tablename (t_record) . "
								WHERE themeid =:themeid and username = :username
										and mobile = :mobile and status=0 and addtime >" . $addtime . " ";
    $row = db_find($sql, array(":themeid"=>$themeid,":username"=>$username,":mobile"=>$mobile));

    if (!empty($row)) {
    	return_msg("您已提交过资料");
    } else {
    	$r = $insertid = db_insert(t_record,$data);
    }
    
    if(!empty($r)){
    	if ($config['formtourl']==0) {
    		return_msg("提交成功");
    	}else{			
		    $sharedata = array(
			    'themeid'=>$config['formtourl'],
		        'fromuid'  => $fromuid,
		        'fopenid'  => $fopenid,
		        'workerid'  => $workerid,
			);
			$tourl = $_G['url_scheme'].$_SERVER['HTTP_HOST']."/".createMobileUrl('index',$sharedata);
		
			echo "<script language='javascript' type='text/javascript'>";
			echo "window.location.href='$tourl';"; 
			echo "</script>";  
			exit;
    	}
	}else{
		return_msg("保存失败请重试");
	}
}
}
}